package com.test.clyq.entity;

import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.test.entity.Item;
import com.test.json.JsonUtil;

/**
 * UsersLog. <br>
 * 管理员日志实体.
 * <p>
 * Copyright: Copyright (c) 2010-4-17 下午05:00:31
 * <p>
 * Company: 北京宽连十方数字技术有限公司
 * <p>
 * Author: liuxl@c-platform.com
 * <p>
 * Version: 1.0
 * <p>
 */
public class UsersLog implements Item {

	private static final long serialVersionUID = 1186504753656L;

	/** 操作后数据 */
	private String afterOperateData;

	/** 操作前数据 */
	private String beforeOperateData;

	/** 从数据库获取workOldData 和 workNewData 进行对比后的数据集合 , 数据保存格式 */
	private List<ContrastingData> contrastingDataList = new ArrayList<ContrastingData>();

	/** 管理员操作网站ID */
	private Long loginWebsiteId;

	/** 数据操作类型 */
	private Long operateDataType;

	/**  */
	private String operateDataTypeStr;

	/** 操作状态 */
	private Long operateStatus;

	/**  */
	private String operateStatusStr;

	/** 对应操作表ID */
	private Long operateTableKey;

	/** 操作时间 */
	private Long operateTime;

	/** 操作类型 */
	private Long operateType;

	/**  */
	private String operateTypeStr;

	/** 使用时间 */
	private Long usedTime;

	/** 用户ID */
	private Long usersId;

	/** 管理员日志ID */
	private Long usersLogId;

	/** 用户登陆名 */
	private String usersLoginName;

	public String getAfterOperateData() {
		return this.afterOperateData;
	}

	public String getBeforeOperateData() {
		return this.beforeOperateData;
	}

	public List<ContrastingData> getContrastingDataList() {
		return this.contrastingDataList;
	}

	public Long getId() {
		return this.usersLogId;
	}

	public Long getLoginWebsiteId() {
		return this.loginWebsiteId;
	}

	public Long getOperateDataType() {
		return this.operateDataType;
	}

	public String getOperateDataTypeStr() {
		return this.operateDataTypeStr;
	}

	public Long getOperateStatus() {
		return this.operateStatus;
	}

	public String getOperateStatusStr() {
		return this.operateStatusStr;
	}

	public Long getOperateTableKey() {
		return this.operateTableKey;
	}

	public Long getOperateTime() {
		return this.operateTime;
	}

	public Long getOperateType() {
		return this.operateType;
	}

	public String getOperateTypeStr() {
		return this.operateTypeStr;
	}

	@SuppressWarnings("unchecked")
	public void getSelf() throws DocumentException {
		List oldElementList = null;
		List newElementList = null;

		if (this.getBeforeOperateData() != null && !"".equals(this.getBeforeOperateData())) {
			Document oldDoc = DocumentHelper.parseText(this.getBeforeOperateData());
			Element oldRoot = oldDoc.getRootElement();
			oldElementList = oldRoot.elements();
		}

		if (this.getAfterOperateData() != null && !"".equals(this.getAfterOperateData())) {
			Document newDoc = DocumentHelper.parseText(this.getAfterOperateData());
			Element newRoot = newDoc.getRootElement();
			newElementList = newRoot.elements();
		}

		int size = 0;

		if (oldElementList != null) {
			size = oldElementList.size();
		} else if (newElementList != null) {
			size = newElementList.size();
		}

		for (int elementI = 0; elementI < size; elementI++) {
			ContrastingData contrastingData = new ContrastingData();
			if (oldElementList != null) {
				Element item = (Element) oldElementList.get(elementI);
				contrastingData.setDataName(item.attributeValue("n"));
				contrastingData.setOldData(replaceHTML(item.getText()));

			}
			if (newElementList != null) {
				Element item = (Element) newElementList.get(elementI);
				contrastingData.setDataName(item.attributeValue("n"));
				contrastingData.setNewData(replaceHTML(item.getText()));
			}

			this.contrastingDataList.add(contrastingData);
		}
		this.setContrastingDataList(this.contrastingDataList);
	}

	public Long getUsedTime() {
		return this.usedTime;
	}

	public Long getUsersId() {
		return this.usersId;
	}

	public String getUsersLoginName() {
		return this.usersLoginName;
	}

	public String replaceHTML(String str) {
		if (str == null) {
			return "";
		}
		return str.replace("<", "&lt;").replace(">", "&gt;");
	}

	public void setAfterOperateData(String afterOperateData) {
		this.afterOperateData = afterOperateData;
	}

	public void setBeforeOperateData(String beforeOperateData) {
		this.beforeOperateData = beforeOperateData;
	}

	public void setContrastingDataList(List<ContrastingData> contrastingDataList) {
		this.contrastingDataList = contrastingDataList;
	}

	public void setId(Long id) {
		this.usersLogId = id;
	}

	public void setLoginWebsiteId(Long loginWebsiteId) {
		this.loginWebsiteId = loginWebsiteId;
	}

	public void setOperateDataType(Long operateDataType) {
		this.operateDataType = operateDataType;
	}

	public void setOperateDataTypeStr(String operateDataTypeStr) {
		this.operateDataTypeStr = operateDataTypeStr;
	}

	public void setOperateStatus(Long operateStatus) {
		this.operateStatus = operateStatus;
	}

	public void setOperateStatusStr(String operateStatusStr) {
		this.operateStatusStr = operateStatusStr;
	}

	public void setOperateTableKey(Long operateTableKey) {
		this.operateTableKey = operateTableKey;
	}

	public void setOperateTime(Long operateTime) {
		this.operateTime = operateTime;
	}

	public void setOperateType(Long operateType) {
		this.operateType = operateType;
	}

	public void setOperateTypeStr(String operateTypeStr) {
		this.operateTypeStr = operateTypeStr;
	}

	public void setUsedTime(Long usedTime) {
		this.usedTime = usedTime;
	}

	public void setUsersId(Long usersId) {
		this.usersId = usersId;
	}

	public void setUsersLoginName(String usersLoginName) {
		this.usersLoginName = usersLoginName;
	}

	public String toJSON() {
		return JsonUtil.getJsonStringFromObject(this);
	}

	public String toXML() {
		return null;
	}
}
